package com.naiterui.ehp.bs.cms.modules.oms.controller;

import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.bo.b2c.order.RefundOrderBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.controller.AbstractController;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.oms.feign.IRefundOrderFeignClient;
import com.naiterui.ehp.bs.cms.modules.oms.service.IOrderService;
import com.naiterui.ehp.bs.cms.modules.oms.service.IRefundOrderService;
import com.naiterui.ehp.bs.cms.modules.oms.vo.RefundOrderDetailVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.RefundOrderVO;
import com.naiterui.ehp.bs.cms.modules.oms.vo.RefundSelectPayOrder;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 *      退款
 * </pre>
 *
 * @author 王泽浩
 * @date 2019/9/29 10:51 上午
 * @since 1.0.0
 */
@Slf4j
@Validated
@Api(tags = {SwaggerTag.CMS + SwaggerTag.ORDER})
@RestController
@AllArgsConstructor
@RequestMapping("refund")
public class RefundOrderController extends AbstractController {

  private final IOrderService orderService;
  private final IRefundOrderService refundOrderService;
  private final IRefundOrderFeignClient refundOrderFeignClient;

  /**
   * 列表
   *
   * @return
   */
  @SysLog("退款列表")
  @GetMapping("/list")
  @ApiOperation("退款列表")
  @PreAuthorize("@p.check('oms:refund:order:list')")
  public PageVO<RefundOrderVO> list(PageParamsVO pageParamsVO, RefundOrderVO refundOrderVO) {
    return this.refundOrderService.queryPage(pageParamsVO, refundOrderVO);
  }

  /**
   * 更新
   */
  @SysLog("更新")
  @PostMapping
  @ApiOperation("更新退款信息")
  @PreAuthorize("@p.check('oms:refund:order:update')")
  public void update(@RequestBody RefundOrderVO refundOrderVO) throws BusinessException {
    RefundOrderBO refundOrder = new RefundOrderBO();
    BeanUtils.copyProperties(refundOrderVO, refundOrder);
    this.refundOrderFeignClient.update(refundOrder, 0);
  }

  /**
   * 更新
   */
  @SysLog("保存/提交")
  @PostMapping("submit")
  @ApiOperation("保存/提交")
  @PreAuthorize("@p.check('oms:refund:order:update')")
  public void updateAndSubmit(@RequestBody RefundOrderVO refundOrderVO) throws BusinessException {
    RefundOrderBO refundOrder = new RefundOrderBO();
    BeanUtils.copyProperties(refundOrderVO, refundOrder);
    this.refundOrderFeignClient.update(refundOrder, 1);
  }

  /**
   * 退款详情
   *
   * @return
   */
  @SysLog("退款详情")
  @GetMapping("/{refundOrderId}")
  @ApiOperation("退款详情")
  @ApiImplicitParam(name = "refundOrderId", value = "退款订单ID", dataType = "Long", paramType = "path", required = true)
  @PreAuthorize("@p.check('oms:refund:order:detai')")
  public RefundOrderDetailVO getById(@PathVariable("refundOrderId") Long refundOrderId) {
    return this.refundOrderService.getRefundOrderDetailById(refundOrderId);
  }

  /**
   * 选择支付订单
   *
   * @return
   */
  @SysLog("选择支付订单")
  @GetMapping("/query/{orderSn}")
  @ApiOperation("选择支付订单")
  @ApiImplicitParam(name = "orderSn", value = "订单号", dataType = "string", paramType = "path", required = true)
  @PreAuthorize("@p.check('oms:refund:order:query')")
  public List<RefundSelectPayOrder> getById(@PathVariable("orderSn") String orderSn) {
    return this.orderService.getPayOrder(orderSn);
  }

  /**
   * 提交订单
   */
  @SysLog("提交订单")
  @PutMapping("/submit")
  @ApiOperation("提交订单")
  @ApiImplicitParam(name = "refundOrderIds", value = "订单ID数组", dataType = "Long", paramType = "body", required = true)
  @PreAuthorize("@p.check('oms:refund:order:submit')")
  public void submit(@RequestBody Set<Long> refundOrderIds) throws BusinessException {
    this.refundOrderFeignClient.submit(refundOrderIds);
  }

  /**
   * 作废订单
   */
  @SysLog("作废订单")
  @PostMapping("/invalid")
  @ApiOperation("作废订单")
  @ApiImplicitParam(name = "refundOrderIds", value = "订单ID数组", dataType = "Long", paramType = "body", required = true)
  @PreAuthorize("@p.check('oms:refund:order:invalid')")
  public void invalid(@RequestBody Set<Long> refundOrderIds) throws BusinessException {
    this.refundOrderFeignClient.invalid(refundOrderIds);
  }

  /**
   * 付款
   */
  @SysLog("付款")
  @PostMapping("/pay")
  @ApiOperation("付款")
  @ApiImplicitParam(name = "refundOrderIds", value = "订单ID数组", dataType = "Long", paramType = "body", required = true)
  @PreAuthorize("@p.check('oms:refund:order:pay')")
  public void pay(@RequestBody Set<Long> refundOrderIds) throws BusinessException {
    this.refundOrderFeignClient.pay(refundOrderIds);
  }

  /**
   * 审核
   */
  @SysLog("审核")
  @PostMapping("/audit")
  @ApiOperation("审核")
  @ApiImplicitParam(name = "refundOrderIds", value = "订单ID数组", dataType = "Long", paramType = "body", required = true)
  @PreAuthorize("@p.check('oms:refund:order:audit')")
  public void audit(@RequestBody Set<Long> refundOrderIds) throws BusinessException {
    this.refundOrderFeignClient.audit(refundOrderIds);
  }

  /**
   * 反审核
   */
  @SysLog("反审核")
  @PostMapping("/reverseAudit")
  @ApiOperation("反审核")
  @ApiImplicitParam(name = "refundOrderIds", value = "订单ID数组", dataType = "Long", paramType = "body", required = true)
  @PreAuthorize("@p.check('oms:refund:order:reverse:audit')")
  public void reverseAudit(@RequestBody Set<Long> refundOrderIds) throws BusinessException {
    this.refundOrderFeignClient.reverseAudit(refundOrderIds);
  }

  /**
   * 删除
   */
  @SysLog("删除")
  @DeleteMapping("{refundOrderId}")
  @ApiOperation("删除")
  @ApiImplicitParam(name = "refundOrderId", value = "退款订单ID", dataType = "Long", paramType = "path", required = true)
  @PreAuthorize("@p.check('oms:refund:order:del')")
  public void del(@PathVariable Long refundOrderId) throws BusinessException {
    this.refundOrderFeignClient.del(refundOrderId);
  }

}
